var readlineSync = require('readline-sync');
var fs = require('fs');
var moduleName = readlineSync.question('please enter you module name!                  ');
var pathName = readlineSync.question('please enter you controller name!              ');

var controllerName = pathName.substring(0, 1).toUpperCase() + pathName.substring(1);
console.log(`start buiding ${controllerName} !`);
var index = 1;
var create = (path, out) => {
    fs.readFile(path, function (err, res) {
        if (err) {
            throw new Error('no such file');
        }
        let controller = res.toString().replace(/name/g, controllerName).replace(/module\//g, moduleName + '/' || '').replace(/path/g, pathName);
        writeController(out, controller);
    })
};
var writeController = (path, content) => {
    fs.open(path, 'r+', function (err, origin) {
        if (err) {
            console.log(err.code);
            if (err.code === 'ENOENT') {

            }
        }
        fs.writeFile(path, content, () => {
            console.log(`build ${Number.parseInt(index / 4 * 100) + "%"} `);
            index++;
        });
    })
};

create('./template/controller.js', './output/controller.js');
create('./template/service.js', './output/service.js');
create('./template/net.js', './output/net.js');
create('./template/config.js', './output/route.js');

